package translateCode
{
	import com.utils.RegUtil;
	
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import spark.components.Label;
	
	import utils.RegUtil;
	import utils.StringTool;
	
	/**
	 * 导出-语言转换，中文转 其它 繁体、韩文
	 */
	public class LangTranslate
	{
		public static var printMsg:Label;
		public static var exportUrl:String;
		
		private var _path:String;
		private var _path2:String;
		private var _cfgStream:FileStream;
		private var _urlLoader:URLLoader;
		private var _timer:Timer;
		
		public function LangTranslate()
		{
			_timer = new Timer(1000);
		}
		public function doLoad(path:String):void
		{
			//"C:\Users\Administrator\Desktop\ani原始\21000\bg_tiles\1.mp"
			
			_path = path;
			_path2 = path.replace(".as", "-222.as");
			
			loadData();
		}
		private function loadData():void
		{
			var request:URLRequest = new URLRequest(_path);
			_urlLoader = new URLLoader();
			_urlLoader.addEventListener(Event.COMPLETE, onComplete1);
			_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onLoadError1);
			_urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onLoadError2);
			_urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadError3);
			_urlLoader.data = null;
			_urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
			_urlLoader.load(request);
			
		}
		/**
		 * 注意，以下转换，只转换logic、modules包里的内容，其它的不要转换。
		 */
		private function onComplete1(e:Event):void
		{
			_urlLoader.removeEventListener(Event.COMPLETE, onComplete1);
			_urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError1);
			_urlLoader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, onLoadError2);
			_urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadError3);
			
			
			if(_path.indexOf("\\BaseAttributeDataBean\.as") != -1){//属性不转换。
				return;
			}
			if(_path.indexOf("\\LoginPanel\.as") != -1){//内网登录选项界面，不转换。
				return;
			}
			var content:String = (_urlLoader.data);
			if(!content){
				return;
			}
			_urlLoader.close();
			
			//文本测试
			var label:TextField = new TextField();
			label.text = content;
//			mytrace("文本行数：" + label.numLines);
			
//			var arr:Array = content.match(/[\u4e00-\u9fa5：，]+/g);
//			var arr:Array = content.match(/\b=| = /g);
			
			var str:String;
			var substr:String;
			var carr:Array;
			var arr2:Array;
			const STAG:String = "\". \[\]";
			var lines:int = label.numLines;
			for(var i:int=0; i<lines; i++){
				str = label.getLineText(i);
				if(StringTool.trim(str)){
					carr = str.match(/[\u4e00-\u9fa5]+/g);
					if(carr.length){//有中文
//						substr = str.substring(str.indexOf(carr[0]));
						carr = str.match(/[\u4e00-\u9fa5，！]+/g);
						//carr = substr.match(/[\u4e00-\u9fa5，！：？、。；,!:?.; \[\]]+/g);
//						carr = substr.match(/[\u4e00-\u9fa5，！？、。；! ]+/g);
						//是注释
						var index1:int = str.indexOf(carr[0]);
						var index2:int = str.indexOf("*");
						var index3:int = str.indexOf("//");
						if(index2 != -1){//判断是否是乘号
							var xingStr:String = str.substring(0, index2);
							xingStr = StringTool.trim(xingStr);
							if(xingStr && xingStr != "/"){
								index2 = -1;
							}
						}
						//检查是否是需要转换的中文
						if((index2 == -1 || index2 > index1) && (index3 == -1 || index3 > index1)){//常规
//						if(index3 != -1 || index2 != -1){//还原注释繁体
							var flag:Boolean = false;
							if(!flag && str.indexOf("myerror(") == -1){ 
								index3 = str.indexOf("mytrace(");
								if(index3 == -1){
									index3 = str.indexOf("Log\.");
									if(index3 == -1){
										index3 = str.indexOf("LazyUtil\.getAttrNameDic");//属性KEY不转。
										if(index3 == -1){
											arr2 = str.match(/\b=| = /g);//有 = 号
											if(arr2.length){
												flag = true;
											}
											
											if(!flag){
												index2 = str.indexOf("(");
												if(index2 < index1){
													flag = true;
												}
											}
										}
									}
								}
							}
							if(flag){//这一行是需要转换的中文
								var jiahaoStr:String = str.substring(index1-4, index1);
								var keyStr:String = "";
								var keyIndex:int = 0;
								if(jiahaoStr.indexOf("+") != -1){
									keyStr += "{0}";
									keyIndex ++;
								}
								for(var j:int=0; j<carr.length; j++){
									if(j == carr.length - 1){
										keyStr += carr[j];
									}
									else{
										keyStr += carr[j] + "{"+keyIndex+"}";
										
//										var isAdd:Boolean = true;
//										if(STAG.indexOf(carr[j]) == -1){
//											if(j > 0){
//												var isC:Array = carr[j].match(/[\u4e00-\u9fa5]+/g);
//												if(!isC.length){
////													var isC2:Array = carr[j-1].match(/[\u4e00-\u9fa5]+/g);
////													if(!isC2.length){
////														isAdd = false;
////													}
//													isAdd = false;
//												}
//											}
//											if(isAdd){
//												keyStr += carr[j] + "{"+keyIndex+"}";
//											}
//										}
									}
									keyIndex ++;
								}
								if(keyStr == "一"){//
									trace("石头在歌唱。");
								}
								var clsArr:Array = _path.split("\\");
								var clsName:String = clsArr[clsArr.length - 1];
								LangTranslateMan.ins.pushLang(keyStr, clsName);
							}
						}
					}
				}
			}
			
//			var newStr:String = content;
//			if(newStr == content){
//				//不需要操作
//				return;
//			}
//			
//			var rootFile:File = new File();
//			var fl:File = rootFile.resolvePath(_path2);
//			
//			try
//			{
//				_cfgStream = new FileStream();
//				_cfgStream.open(fl, FileMode.WRITE);
//				_cfgStream.writeUTFBytes(newStr);
//				_cfgStream.close();
//				
//				_timer.addEventListener(TimerEvent.TIMER, onTimer2);
//				_timer.start();
//			}
//			catch(e:Error)
//			{
//				throw new Error(e.getStackTrace());
//			}
		}
		
		
		
		private function onTimer2(e:TimerEvent):void
		{
			var rootFile:File = new File();
			var fnew:File = rootFile.resolvePath(_path2);
			if(fnew.exists){
				
				_timer.removeEventListener(TimerEvent.TIMER, onTimer2);
				_timer.stop();
				_timer = null;
				
				var rootFile:File = new File();
				var fold:File = rootFile.resolvePath(_path);
				
				fnew.moveTo(fold, true);
			}
		}
		
		private function onLoadError1(e:Event):void
		{
			trace("加载是失败");
		}
		private function onLoadError2(e:HTTPStatusEvent):void
		{
			if(e.status >= 400){
				trace("加载是失败2,url="+e.target.url);
			}
		}
		private function onLoadError3(e:Event):void
		{
			trace("安全沙箱错误");
		}
		
		public static var dicIdCount:int = 0;
	}
}